package com.sjksy.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjksy.demo.domain.Employee;
import com.sjksy.demo.domain.EmployeeType;
import com.sjksy.demo.domain.Guide;
import com.sjksy.demo.dto.guide.GuideDto;
import com.sjksy.demo.mapper.BranchMapper;
import com.sjksy.demo.mapper.GuideMapper;
import com.sjksy.demo.msmapper.employee.EmployeeMsMapper;
import com.sjksy.demo.service.EmployeeService;
import com.sjksy.demo.mapper.EmployeeMapper;
import com.sjksy.demo.vo.employee.EmployeeRequestVo;
import com.sjksy.demo.vo.employee.EmployeeResponseVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author guyueyuan
* @description 针对表【employee】的数据库操作Service实现
* @createDate 2024-12-11 15:37:02
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private GuideMapper guideMapper;
    @Resource
    private EmployeeMsMapper employeeMsMapper;
    @Resource
    private BranchMapper branchMapper;

    /**
     * 插入一条员工数据，涉及员工表，和导游表
     * @param employeeRequestVo
     */
    public void insert(EmployeeRequestVo employeeRequestVo) {
        Integer empTyid= employeeRequestVo.getEmpTyId();
            employeeMapper.insert(employeeRequestVo);

        //单独插入导游
        if(empTyid.equals(EmployeeType.GUIDE)){
            Guide guide=Guide.builder()
                    .guideNumber(employeeRequestVo.getGuideNumber())
                    .guideLevel(Guide.LEVEL)
                    .employeeId(employeeRequestVo.getEmployeeId())
                    .build();
            guideMapper.insert(guide);
        }
    }

    /**
     * 根据id删除员工
     * @param id
     * todo 目前删除功能是有问题的
     */
    @Transactional
    public void delete(Integer id) {
        //导游表中删除
        guideMapper.delete(id);
        //员工表中删除
        employeeMapper.delete(id);
    }

    /**
     * 查询所有员工信息
     * @return
     */
    public List<EmployeeResponseVo> searchAll() {
        List<Employee> employeeList=employeeMapper.getAll();
        List<EmployeeResponseVo> employeeResponseVos=new ArrayList<>();

        for (Employee employee:employeeList){
            EmployeeResponseVo employeeResponseVo=employeeMsMapper.entityToEmployeeVo(employee);
            String name=branchMapper.getName(employee.getBranchId());
            employeeResponseVo.setBranchName(name);

            if(employee.getEmpTyId().equals(EmployeeType.MANAGER)){
                employeeResponseVo.setEmpType("经理");
            }
            else {
                employeeResponseVo.setEmpType("导游");

                GuideDto guideDto = guideMapper.getNumAndLev(employee.getEmployeeId());
                employeeResponseVo.setGuideNumber(guideDto.getGuideNumber());
                employeeResponseVo.setGuideLevel(guideDto.getGuideLevel());
            }
            employeeResponseVos.add(employeeResponseVo);
        }
        return employeeResponseVos;
    }
}




